package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lst.api.entity.AssetsCarInfo;
import com.lst.api.entity.AssetsCreditInfo;
import com.lst.api.mapper.AssetsCreditInfoMapper;
import com.lst.api.service.IAssetsCreditInfoService;
import com.lst.api.vo.AssetsCreditInfoVo;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 资产信息中的信用信息业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class AssetsCreditInfoService extends BaseService<AssetsCreditInfoMapper, AssetsCreditInfo> implements IAssetsCreditInfoService {

    @Override
    public boolean saveOrUpdate(long userId, String applyNo, AssetsCreditInfoVo assetsCreditInfoVo) {
        if (assetsCreditInfoVo == null) {
            return false;
        }
        assetsCreditInfoVo.check();
        AssetsCreditInfo assetsCreditInfo = new AssetsCreditInfo(userId, applyNo, assetsCreditInfoVo);
        QueryWrapper<AssetsCreditInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(AssetsCreditInfo.APPLY_NO, applyNo)
                .eq(AssetsCreditInfo.PROMO_USER_ID, userId);
        int ret = super.saveNotExist(assetsCreditInfo, queryWrapper);
        if (ret == 0) {
            UpdateWrapper<AssetsCreditInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .eq(AssetsCreditInfo.APPLY_NO, applyNo)
                    .eq(AssetsCreditInfo.PROMO_USER_ID, userId)
                    .set(AssetsCreditInfo.UPDATE_AT, new Date())
                    .set(AssetsCreditInfo.CREDIT_CARD_SUM, assetsCreditInfo.getCreditCardSum())
                    .set(AssetsCreditInfo.CREDIT_CARD_TOTAL_QUOTA, assetsCreditInfo.getCreditCardTotalQuota())
                    .set(AssetsCreditInfo.CREDIT_CARD_TOTAL_USE_QUOTA, assetsCreditInfo.getCreditCardTotalUseQuota());
            return super.update(updateWrapper);
        }
        return true;
    }

    @Override
    public AssetsCreditInfo getByApplyNo(String applyNo) {
        QueryWrapper<AssetsCreditInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(AssetsCreditInfo.APPLY_NO, applyNo);
        return super.getOne(queryWrapper);
    }
}